<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 注意: 💥 计算属性是一个计算得来的值, 不能当做函数调用 -->
      <!-- 注意: 💥 计算属性不能和data中变量名重命 -->
      <!-- 💥 计算属性比函数有优势 -->
      <!-- 计算属性会自动缓存计算的结果, 比函数减少运算次数 -->
      <h1>总年龄: {{getAge()}}</h1>
      <h1>总年龄: {{getAge()}}</h1>
      <h1>总年龄: {{getAge()}}</h1>
      <h1>总年龄: {{totalAge}}</h1>
      <h1>总年龄: {{totalAge}}</h1>
      <h1>总年龄: {{totalAge}}</h1>
    </div>

    <script src="./vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          list: [
            { id: 1, name: "张三", age: 18 },
            { id: 2, name: "李四", age: 29 },
            { id: 3, name: "王五", age: 30 },
          ],
          // totalAge: 200,
        },
        methods: {
          getAge() {
            console.log("getAge函数执行了计算  ----->  ");
            return this.list.reduce((pre, item) => {
              return pre + item.age;
            }, 0);
          },
        },
        computed: {
          totalAge() {
            console.log("totalAge函数执行了计算  ----->  ");
            return this.list.reduce((pre, item) => {
              return pre + item.age;
            }, 0);
          },
        },
      });
    </script>
  </body>
</html>
